<template>
	<div class="modify-dialog">
		<slot> </slot>
		<el-dialog :close-on-click-modal="false" v-bind="dialogOpts" :visible.sync="dialogVisible">
			<Form v-bind="formOpts" v-on="formEvent">
				<template #formSuf>
					<el-form-item label="回款截图" style="width: 100%">
<!--						<Uploader v-model="fileList" />-->
            <span style="color: gray; font-size: 12px"
            >(粘贴复制即可上传图片)</span
            >
            <div class="div" style="display: flex; align-items: center">
              <div
                  v-for="(v, i) in fileList"
                  style="display: flex; align-items: center; margin-right: 10px"
              >
                <el-image
                    :key="i"
                    style="width: 50px; height: 50px"
                    ref="preview"
                    :src="host + v"
                    :preview-src-list="[host + v]"
                >
                </el-image>
                <div @click="deletePaiedUrl(i)">
                  <el-icon name="delete"></el-icon>
                </div>
              </div>
            </div>
					</el-form-item>
				</template>
				<template #formBtn>
					<div class="dialog-footer">
						<el-button @click="dialogVisible = false">取 消</el-button>
						<el-button type="primary" @click="onConfirm"> 确 定 </el-button>
					</div>
				</template>
			</Form>
		</el-dialog>
	</div>
</template>

<script>
import modifyMixins from "@/mixins/modifyMixins.vue";
import axios from "axios";
import store from "../../../store";

export default {
	mixins: [modifyMixins],
	data () {
		return {
			dialogTitle: '回款',
			labelPosition: "left",
      host: this.$imgHost,
			form: {
				amount: {
					tag: "el-input",
					label: "回款金额",
					value: "",
					props: {
						type: "number",
						min: 0,
						placeholder: "请输入回款金额",
					},
				},
			},
		};
	},
	computed: {
    preImgs() {
      return this.fileList.map((e) => `${this.$imgHost}${e}`);
    },
  },
  created() {
    document.addEventListener('paste', this.handlePaste)
  },
  methods: {
    handlePaste (event) {
      if (!this.dialogVisible) return;
      const clipboardData = event.clipboardData || window.clipboardData;
      if (clipboardData) {
        for (let i = 0; i < clipboardData.items.length; i++) {
          const item = clipboardData.items[i];

          if (item.kind === 'file' && item.type.startsWith('image/')) {
            const file = item.getAsFile();
            if (file) {
              this.uploadImage(file);
            }
          } else {
            this.$message.error('请拷贝图片!');
            break;
          }
        }
      }
    },
    deletePaiedUrl(i) {
      this.fileList.splice(i, 1)

    },
    uploadImage (imageFile) {
      const formData = new FormData();
      formData.append('file', imageFile);
      axios.post(`${this.$apiHost}/common/fileImgUrl`, formData, {
        headers: {
          'Content-Type': 'multipart/form-data',
          'token':store.state.token
        }
      })
          .then(response => {
            this.fileList.push(response.data.result[0]);
            // this.preSalesImgList.push('https://' + response.data.result[0]);
          })
          .catch(error => {
          });
    },
		onSubmit (form) {
			const { modify, row, fileList } = this;
			if (!form.amount) return this.$message.error("请输入金额");
			if (!fileList[0]) return this.$message.error("请上传图片");
			form.prove = fileList[0];
			form.orderId = row.id;
			
			// newPaymentCreate
			this.$api["orderAddPayment"](form).then(this.submitDoneMsg);
		},
	},
};
</script>

<style lang="less" scoped></style>

